﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using TeBigCoreApi.Common;
using TeBigCoreApi.Models;

namespace TeBigCoreApi.Controllers
{
    [Produces("application/json")]
    [Route("api/Case")]
    public class CaseController : Controller
    {
        string[] _case_type = new[]
        {
            "治安", "刑事", "其他"
        };

        string[] _case_reason = new[]
        {
            "侵犯财产案",
            "侵犯权利案",
            "妨害社会秩序案",
            "危害公共安全案",
            "毒品案",
        };

        string[] _year_type = new[]
        {
            "今年", "同比"
        };

        [Route("Pie")]
        [HttpPost]
        public QueryCaseStatPieResponse QueryCaseStatPie([FromBody]QueryCaseStatPieRequest req)
        {
            var resp = new QueryCaseStatPieResponse();

            try
            {
                foreach (var item in _case_type)
                {
                    resp.List.Add(new CaseStatPie
                    {
                        AxisX = item,
                        AxisY = MyRandom.NextInt(50, 150),
                    });
                }

                resp.OnSuccess();
            }
            catch (Exception ex)
            {
                resp.OnException(ex);
            }

            return resp;
        }

        [Route("Line")]
        [HttpPost]
        public QueryCaseStatLineResponse QueryCaseStatLine([FromBody]QueryCaseStatLineRequest req)
        {
            var resp = new QueryCaseStatLineResponse();

            var dt = DateTime.Now;

            try
            {
                foreach (var c in _case_type)
                {
                    for (int i = 12; i >= 1; i--)
                    {
                        var m = dt.AddMonths(-1 * i);

                        foreach (var y in _year_type)
                        {
                            resp.List.Add(new CaseStatLine
                            {
                                AxisX = $"{m.Month}月",
                                AxisY = MyRandom.NextInt(50, 150),
                                Type = c,
                            });
                        }
                    }
                }

                resp.OnSuccess();
            }
            catch (Exception ex)
            {
                resp.OnException(ex);
            }

            return resp;
        }

        [Route("Bar")]
        [HttpPost]
        public QueryCaseStatBarResponse QueryCaseStatBar([FromBody]QueryCaseStatBarRequest req)
        {
            var resp = new QueryCaseStatBarResponse();

            try
            {
                foreach (var c in _case_type)
                {
                    foreach (var s in DeptManager.Stations)
                    {
                        resp.List.Add(new CaseStatBar
                        {
                            AxisX = s.Name,
                            AxisY = MyRandom.NextInt(20, 999),
                            Type = c,
                        });
                    }                   
                }

                resp.OnSuccess();
            }
            catch (Exception ex)
            {
                resp.OnException(ex);
            }

            return resp;
        }

        [Route("Feedback")]
        [HttpPost]
        public QueryCaseFeedbackResponse QueryCaseFeedback([FromBody]QueryCaseFeedbackRequest req)
        {
            var resp = new QueryCaseFeedbackResponse();

            try
            {
                foreach (var s in DeptManager.Stations)
                {
                    resp.List.Add(new CaseFeedback
                    {
                        AxisX = s.Name,
                        AxisY = MyRandom.NextDouble(0, 1),
                    });
                }

                resp.OnSuccess();
            }
            catch (Exception ex)
            {
                resp.OnException(ex);
            }

            return resp;
        }

        [Route("Major")]
        [HttpPost]
        public QueryCaseMajorResponse QueryCaseMajor([FromBody]QueryCaseMajorRequest req)
        {
            var resp = new QueryCaseMajorResponse();

            try
            {
                var station = DeptManager.Stations[MyRandom.NextInt(0, DeptManager.Stations.Count)];
                var reason = _case_reason[MyRandom.NextInt(0, _case_reason.Length)];

                resp.List.Add(new CaseMajor
                {
                    Coord = new CoordData { Longitude = station.Longitude, Latitude = station.Latitude },
                    Dept = station.Name,
                    DateTime = DateTime.Now,
                    Info = reason,
                });

                resp.OnSuccess();
            }
            catch (Exception ex)
            {
                resp.OnException(ex);
            }

            return resp;
        }
    }
}